package com.jiutong.meipenggang.utils;


import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * 作用:
 * Created by weicheng on 2017/6/5.
 */

public class NumUilt {

    //超过一万的用万单位表示并保留2位小数
    public static String doubleFormat(String s){
        double d =Double.parseDouble(s);
        DecimalFormat df = new DecimalFormat("0.00");
        if(d<10000) {
            return df.format(d);
        }else{
            return df.format(d/10000);
        }
    }

    //超过一万的用万单位表示并保留2位小数
    public static String doubleWanFormat(String s){
        double d =Double.parseDouble(s);
        DecimalFormat df = new DecimalFormat("0");
        if(d<10000) {
            return df.format(d);
        }else{
            return df.format(d/10000)+"万";
        }
    }

    public static String formatFloat(float num){
        double d =Double.parseDouble(num+"");
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        return decimalFormat.format(d);
    }

    public static String formatDouble(Double num){
        double d =Double.parseDouble(num+"");
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        return decimalFormat.format(d);
    }

    public static String formatString(String num){
        double d =Double.parseDouble(num+"");
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        return decimalFormat.format(d);
    }

    public static String formatInt(String num){
        double d =Double.parseDouble(num+"");
        DecimalFormat decimalFormat = new DecimalFormat("0");
        return decimalFormat.format(d);
    }


    //手机中间四位隐藏格式
    public static String phoneFormat(String phone){
        //默认11位
        StringBuffer buffer = new StringBuffer(phone);
        return buffer.replace(3,7,"****").toString();
    }

    /***
     * 保留2位小数
     * 四舍五入
     * @param
     *
     * @return
     * 返回一个double类型的2位小数
     */
    public static Double get2Double(Double doubleVal,int scale){
        if(null == doubleVal){
            doubleVal = new Double(0);
        }
        return new BigDecimal(doubleVal).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /***
     * 格式化Double类型并保留scale位小数
     * 四舍五入
     * @param doubleVal
     * @param scale
     * scale必须为大于0的正整数，不能等于0
     * @return
     */
    public static String formatBy2Scale(Double doubleVal,int scale){
        if(null == doubleVal){
            doubleVal = new Double(0);
        }
        StringBuffer sbStr = new StringBuffer("0.");
        for (int i = 0; i < scale; i++) {
            sbStr.append("0");
        }
        DecimalFormat myformat = new DecimalFormat(sbStr.toString());
        return myformat.format(doubleVal);
    }

    /***
     * Double类型相加 <font color="red">+</font><br/>
     * ROUND_HALF_UP <font color="red">四舍五入</font><br/>
     * @param val1
     *
     * @param val2
     *
     * @param scale
     * <font color="red">保留scale位小数</font><br/>
     * @return
     */
    public static Double add(Double val1,Double val2,int scale){
        if(null == val1){
            val1 = new Double(0);
        }
        if(null == val2){
            val2 = new Double(0);
        }
        return new BigDecimal(Double.toString(val1)).add(new BigDecimal(Double.toString(val2))).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /***
     * Double类型相减 <font color="red">—</font><br/>
     * ROUND_HALF_UP <font color="red">四舍五入</font><br/>
     * @param val1
     *
     * @param val2
     *
     * @param scale
     * <font color="red">保留scale位小数</font><br/>
     * @return
     */
    public static Double subtract(Double val1,Double val2,int scale){
        if(null == val1){
            val1 = new Double(0);
        }
        if(null == val2){
            val2 = new Double(0);
        }
        return new BigDecimal(Double.toString(val1)).subtract(new BigDecimal(Double.toString(val2))).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /***
     * Double类型相乘 <font color="red">*</font><br/>
     * ROUND_HALF_UP <font color="red">四舍五入</font><br/>
     * @param val1
     *
     * @param val2
     *
     * @param scale
     * <font color="red">保留scale位小数</font><br/>
     * @return
     */
    public static Double multiply(Double val1,Double val2,int scale){
        if(null == val1){
            val1 = new Double(0);
        }
        if(null == val2){
            val2 = new Double(0);
        }
        return new BigDecimal(Double.toString(val1)).multiply(new BigDecimal(Double.toString(val2))).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /***
     * Double类型相除 <font color="red">/</font><br/>
     * ROUND_HALF_UP <font color="red">四舍五入</font><br/>
     * @param val1
     *
     * @param val2
     *
     * @param scale
     * <font color="red">保留scale位小数</font><br/>
     * @return
     */
    public static Double divide(Double val1,Double val2,int scale){
        if(null == val1){
            val1 = new Double(0);
        }
        if(null == val2 || val2 == 0){
            val2 = new Double(1);
        }
        return new BigDecimal(Double.toString(val1)).divide(new BigDecimal(Double.toString(val2))).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }


    /***
     * Double类型取余    <font color="red">%</font><br/>
     * ROUND_HALF_UP <font color="red">四舍五入</font><br/>
     * @param val1
     *
     * @param val2
     *
     * @param scale
     * <font color="red">保留scale位小数</font><br/>
     * @return
     */
    public static int divideAndRemainder(Double val1,Double val2,int scale){
        if(null == val1){
            val1 = new Double(0);
        }
        if(null == val2 || val2 == 0){
            val2 = new Double(1);
        }
        return new BigDecimal(Double.toString(val1)).divideAndRemainder(new BigDecimal(Double.toString(val2)))[1].setScale(scale, BigDecimal.ROUND_HALF_UP).intValue();
    }


}
